package cn.trevet.minecraft.limitation;

import cn.trevet.minecraft.limitation.event.TrevetEventBase;
import cn.trevet.minecraft.limitation.vo.NearingBase;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;

public final class Limitation extends JavaPlugin implements Listener {
    private static final String PREFIX_DEFAULT = "data.no-nearing.block";
    private static final String PREFIX_A = "prefixA";
    private static final String PREFIX_B = "prefixB";
    private static final String REJECT_MESSAGES = "reject-messages";
    private boolean debug = false;
    private boolean enabled = false;


    @Override
    public void onEnable() {
        this.init();
    }

    /**
     * 初始化配置项相关操作
     */
    private void init() {
        try {
            // Plugin startup logic
            this.getLogger().info("initialization");

            if (this.getConfig().getBoolean("debug")) {
                this.debug = true;
            }

            // 判断配置文件是否存在.
            if (Objects.isNull(this.getConfig().get("version"))) {
                this.getConfig().set("version", "1.0");
                this.getConfig().set("debug", false);
                this.getConfig().set("enabled", false);

                List<Map<String, Object>> map = new ArrayList<>();
                Map<String, Object> mapNearing = new HashMap<>();
                mapNearing.put(PREFIX_A, Collections.singletonList("BLOCKBOOSTER_BOOSTER_"));
                mapNearing.put(PREFIX_B, Collections.singletonList("TORCHERINO_BLOCK"));
                mapNearing.put(REJECT_MESSAGES, "你干嘛！哎哟！");
                map.add(mapNearing);
                this.getConfig().set(PREFIX_DEFAULT, map);
                this.saveConfig();
            }

            // 判断调试模式
            if (this.getConfig().getBoolean("debug")) {
                this.debug = true;
            }

            // 插件是否未启用
            if (!this.getConfig().getBoolean("enabled")) {
                return;
            }
            this.enabled = true;
            // 新建本插件对应的事件监听器
            TrevetEventBase trevetEvent = new TrevetEventBase(this.enabled, this.debug, this);

            List<Map<String, List<String>>> list = (List<Map<String, List<String>>>) this.getConfig().getList(PREFIX_DEFAULT);

            // 将两个前缀作为查询条件
            list.forEach(s -> {
                NearingBase nearingBase = new NearingBase();
                nearingBase.setPrefixA(s.get(PREFIX_A));
                nearingBase.setPrefixB(s.get(PREFIX_B));

                Object rejectMessage = s.get(REJECT_MESSAGES);
                nearingBase.setRejectMessages(rejectMessage.toString());

                // 遍历两个前缀,将前缀A的对立的前缀B作为索引
                for (String string : nearingBase.getPrefixB()) {
                    trevetEvent.getNearingEntityMap().put(string, nearingBase);
                }
                for (String string : nearingBase.getPrefixA()) {
                    trevetEvent.getNearingEntityMap().put(string, nearingBase);
                }
            });

            this.getLogger().config("Plugin startup");
            this.getServer().getPluginManager().registerEvents(trevetEvent, this);

        } catch (Exception e) {
            this.getLogger().fine("There is an error during initialization");
            // 防止事件运行
            this.onDisable();
        }

    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        this.getServer().getPluginManager().disablePlugin(this);
        this.enabled = false;
        this.getLogger().info("Plugin Disabled");
    }

}
